package com.sanhe.rfmuscle.activity.work;

import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;


import com.sanhe.rfmuscle.App;
import com.sanhe.rfmuscle.databinding.ActivityPwdBinding;
import com.sanhe.rfmuscle.event.Event;
import com.sanhe.rfmuscle.event.PwdEvent;
import com.sanhe.rfmuscle.view.PwdEditText;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class PwdActivity extends BaseWorkActivity {
    Typeface medium = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Medium.ttf");
    ActivityPwdBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPwdBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.back.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x20));

        binding.title.setTypeface(medium);
        binding.et.setKeyEventClickListener(new PwdEditText.KeyEventClickListener() {
            @Override
            public void sendData(int key) {
                sendTouchEvent((byte) 0, (byte) (0x60 + key));
            }

            @Override
            public void enter() {
                sendTouchEvent((byte) 1, (byte) 0x6c);
            }

            @Override
            public void backspace() {
                sendTouchEvent((byte) 1, (byte) 0x6b);
            }
        });
        updatePwd();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PwdEvent event) {
        updatePwd();
    }

    private void updatePwd() {
        int j = Event.pwdEvent.getValue();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < j; i++) {
            sb.append("*");
        }
        binding.et.setText(sb.toString());
    }

    @Override
    public void sendTouchEvent(byte data_H, byte data_L) {
        super.sendTouchEvent(data_H, data_L);
    }
}
